package com.lxmes.business.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.lxmes.business.domain.CcChukuInfo;
import com.lxmes.business.domain.CcStockInfo;
import com.lxmes.business.mapper.CcChukuInfoMapper;
import com.lxmes.business.mapper.CcStockInfoMapper;
import com.lxmes.common.exception.ServiceException;
import com.lxmes.common.utils.DateUtils;
import com.lxmes.common.utils.NewNumber;
import com.lxmes.common.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.business.mapper.CcPlanItemGetMapper;
import com.lxmes.business.domain.CcPlanItemGet;
import com.lxmes.business.service.ICcPlanItemGetService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 物料领用计划Service业务层处理
 * 
 * @author 张志煜
 * @date 2023-01-15
 */
@Service
public class CcPlanItemGetServiceImpl implements ICcPlanItemGetService 
{
    @Autowired
    private CcPlanItemGetMapper ccPlanItemGetMapper;
    @Autowired
    private CcChukuInfoMapper chukuInfoMapper;
    @Autowired
    private CcStockInfoMapper stockInfoMapper;
    /**
     * 查询物料领用计划
     * 
     * @param id 物料领用计划主键
     * @return 物料领用计划
     */
    @Override
    public CcPlanItemGet selectCcPlanItemGetById(Long id)
    {
        return ccPlanItemGetMapper.selectCcPlanItemGetById(id);
    }

    /**
     * 查询物料领用计划列表
     * 
     * @param ccPlanItemGet 物料领用计划
     * @return 物料领用计划
     */
    @Override
    public List<CcPlanItemGet> selectCcPlanItemGetList(CcPlanItemGet ccPlanItemGet)
    {
        return ccPlanItemGetMapper.selectCcPlanItemGetList(ccPlanItemGet);
    }

    /**
     * 新增物料领用计划
     * 
     * @param ccPlanItemGet 物料领用计划
     * @return 结果
     */
    @Override
    public int insertCcPlanItemGet(CcPlanItemGet ccPlanItemGet)
    {
        ccPlanItemGet.setCreateTime(DateUtils.getNowDate());
        return ccPlanItemGetMapper.insertCcPlanItemGet(ccPlanItemGet);
    }

    @Override
    @Transactional
    public int addScChuKu( Long[] ids) {
        String number= "CK-"+ NewNumber.createNewNumber(chukuInfoMapper.getNewCode(DateUtils.getDate()));
        for (int i = 0; i <ids.length ; i++) {
            CcPlanItemGet get = ccPlanItemGetMapper.selectCcPlanItemGetById(ids[i]);
            get.setCreateTime(DateUtils.getNowDate());
            CcChukuInfo info=new CcChukuInfo();
            BeanUtils.copyProperties(get,info);
            info.setDanhao(number);
            info.setChukuDate(new Date());
            info.setChukuType(4L);
            info.setOrderType(get.getOrderType());
            info.setOrderDanhao(get.getOrderDanhao());
            info.setJhckNum(get.getJhNum());
            info.setCreateTime(DateUtils.getNowDate());
            info.setCreateBy(SecurityUtils.getUsername());
            List<CcStockInfo> stockinfoList = stockInfoMapper.selectCcStockInfoListByItemId(info.getItemId());
            if(stockinfoList.size()>0){
                BigDecimal ckNum = get.getJhNum();
                //可用数量总和
                BigDecimal jsNum=new BigDecimal("0");
                for (CcStockInfo stockInfo:stockinfoList){
                    BigDecimal kyNum=stockInfo.getKcNum().subtract(stockInfo.getLockNum());
                    jsNum=jsNum.add(kyNum);
                    //如果第一条库存够的话直接扣除
                    if(kyNum.compareTo(ckNum)==1){
                        stockInfo.setKcNum(stockInfo.getKcNum().subtract(ckNum));
                        stockInfo.setKyNum(stockInfo.getKcNum().subtract(stockInfo.getLockNum()));
                        stockInfoMapper.updateCcStockInfo(stockInfo);
                        break;
                        //否则循环减去
                    }else {
                        ckNum=ckNum.subtract(kyNum);
                        stockInfo.setKcNum(stockInfo.getKcNum().subtract(kyNum));
                        stockInfo.setKyNum(stockInfo.getKcNum().subtract(stockInfo.getLockNum()));
                        stockInfoMapper.updateCcStockInfo(stockInfo);
                    }
                }
                if(ckNum.compareTo(jsNum)==1){
                    throw new ServiceException(info.getItemId()+"库存不足报工失败！！！");
                }
            }else {
                throw new ServiceException(info.getItemId()+"库存不足报工失败！！！");
            }
             chukuInfoMapper.insertCcChukuInfo(info);
        }
        return 1;
    }

    /**
     * 修改物料领用计划
     * 
     * @param ccPlanItemGet 物料领用计划
     * @return 结果
     */
    @Override
    public int updateCcPlanItemGet(CcPlanItemGet ccPlanItemGet)
    {
        ccPlanItemGet.setUpdateTime(DateUtils.getNowDate());
        return ccPlanItemGetMapper.updateCcPlanItemGet(ccPlanItemGet);
    }

    /**
     * 批量删除物料领用计划
     * 
     * @param ids 需要删除的物料领用计划主键
     * @return 结果
     */
    @Override
    public int deleteCcPlanItemGetByIds(Long[] ids)
    {
        return ccPlanItemGetMapper.deleteCcPlanItemGetByIds(ids);
    }

    /**
     * 删除物料领用计划信息
     * 
     * @param id 物料领用计划主键
     * @return 结果
     */
    @Override
    public int deleteCcPlanItemGetById(Long id)
    {
        return ccPlanItemGetMapper.deleteCcPlanItemGetById(id);
    }
}
